<?php
namespace app\controller\admin;

use app\BaseController;
use think\facade\Request;
// use think\facade\Cache;
use app\middleware\UniAccount;
use app\model\CategoryModel;

class Category extends BaseController
{

    // protected $middleware  = [
    //     UniAccount::class    => [], 
    // ];

    public function tree(){


        $tree = CategoryModel::tree2();

        return json( $tree );
    }

    public function index( Request $request ){
        
        
        $filter = [];
        $filter['name'] = Request::param('name');
        if( isset( $filter['name'] ) ){

            $filter['name'] = Request::param('name');

        }

        if( isset($_GET['status']) ){
            $filter['status'] = $_GET['status'];
        }
        if( isset($_GET['category_ids']) ){
            $filter['category_ids'] = $_GET['category_ids'];
        }

        $list = CategoryModel::get_path_list( $filter );

        echo json_encode( $list );


    }

    public function single(){

        $id = $_GET['id'];

        $category = CategoryModel::single( $id );

        $paths = CategoryModel::get_category_path( $id );

        $path_name = [];
        // $path_arr = [];
        foreach( $paths as $path ){

            if( $path['path_id'] == $id ){
                break;
            }
            // $path_arr[] = $path;
            $path_name[] = $path['name'];
        }

        // $category['path'] = implode($path_name, '>');
        $category['path'] = implode( '>', $path_name);
        // $category['path_arr'] = $path_arr;
        // echo json_encode( $category );
        return json($category) ;

    }

    public function search(){

        $filter = [];
        if( !empty( $_GET['filter']) ) {
            $filter['name'] = $_GET['filter'];
        }

        $list = CategoryModel::get_path_list( $filter );

        return json( $list );

    }

    public function remove( Request $request ){

        $res= [
            'code'=>0,
        ];

        $category_id = Request::param( 'id' );
        if( empty( $category_id ) ){

            $res= [
                'code'=>1,
                'message'=>'请选中你要删除的分类',
            ];
            return json( $res );

            
        }

        //todo 查看是否有子分类，有则报错
        $parent = CategoryModel::hasChildCategory( $category_id );

        if( !empty($parent) ){

            $res['code'] = 1;
            $res['category_id'] = $category_id;
            $res['message'] = '分类下有子分类,无法删除';
        }else{

            //todo 查看其下是否挂有产品， 有则报错

            //todo 删除分类
            CategoryModel::remove( $category_id );
            CategoryModel::remove_category_path( $category_id );
        }

        return json( $res );

    }


    public function edit(){

        $category_id = $_GET['category_id'];

        $parent_id = $_POST['parent_id'];

        $name = $_POST['name'];
        $sort_order = $_POST['sort_order'];
        $status = $_POST['status'];
        $image = $_POST['image'];


        $res['code'] = 0;
        $data = [
            'name'      => $name,
            'sort_order'=> $sort_order,
            'status'    => $status,
            'parent_id' => $parent_id,
            'image'     => $image,
        ];
        //todo 修改 category

        if( $category_id ){
            $res['ret'] = CategoryModel::edit( $category_id, $data );
        }else{
            $category_id = CategoryModel::createOne( $data );
        }

        //todo 找到 category_path 关系, 删除并重新，并插入path
        CategoryModel::remove_category_path( $category_id);
        CategoryModel::set_category_path( $category_id, $parent_id );
        
        $res['category_id'] = $category_id;
        echo json_encode( $res );

    }
}